package work.timejay.user.controller.files;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.jsf.FacesContextUtils;
import org.springframework.web.multipart.MultipartFile;
import work.timejay.common.ApiResult;
import work.timejay.utils.FileUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * @className:FileController.java
 * @author:王文杰
 * @version:1.0.0
 * @description:TODO
 * @createTime 2023年11月23日 17:40:00
 */
@RestController
@RequestMapping(value = "/file")
@Tag(name = "文件接口层",description = "FileController")
public class FileController {

    @Autowired
    private FileUtils fileUtils;

    /**
     * 文件上传
     */
    @PostMapping
//    @ApiOperation(value = "文件上传接口")
//    @SaIgnore
    public ApiResult<String> upload(@RequestPart(value = "file") MultipartFile file) {
        String filePath = fileUtils.upload(file);
        return ApiResult.success(filePath);
    }

    /**
     * 查看文件
     */
    @GetMapping
    public ApiResult<String> show(@RequestParam(value = "fileName") String fileName) {
        String url = fileUtils.preview(fileName);
        return ApiResult.success(url);
    }

    /**
     * 下载文件
     */
    @GetMapping(value = "/download")
    public ApiResult<Void> download(@RequestParam(value = "fileName") String fileName, HttpServletResponse response) {
        fileUtils.download(fileName, response);
        return ApiResult.success();
    }
}
